package com.xiaoshangkou.hellopra.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.pojo.Permission;
import com.xiaoshangkou.hellopra.service.PermissionServeice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/permission/")
public class PermissionController {
    @Autowired
    private PermissionServeice permissionServeice;

    /**
     * 用途：进入权限页面时分页查询所有权限
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size){
        ModelAndView modelAndView=new ModelAndView();
        Page<Permission> permissionPage = permissionServeice.findPermission(page, size);
        modelAndView.addObject("permissionPage",permissionPage);
        modelAndView.setViewName("/backstage/permission_all");
        return modelAndView;
    }


    /**
     * 用途：添加新的权限
     * permission :前端传来的权限对象
     * 待更新：后期前端页面通过json数据实现局部更新
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add(Permission permission){
        permissionServeice.add(permission);
        return "添加成功";
    }

    /**
     * 用途：删除角色
     * @param peid 前端传来的要删除角色的peid
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer peid){
        permissionServeice.delete(peid);
        return "redirect:/backstage/permission/all";
    }

    /**
     *用途：在修改管理员之前，先将原来的数据返回到前端
     *peid:权限的peid
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Permission edit(Integer peid){
        Permission permission = permissionServeice.findById(peid);
        return permission;
    }


    @RequestMapping("/update")
    public String update(Permission permission){
        permissionServeice.update(permission);
        return "redirect:/backstage/permission/all";
    }




}
